09. Intro to Fragments

Intro to Fragments

Intro to Fragments

A fragment is just a part of an activity. You can have a fragment that takes up part of a screen or a whole screen. Or you can show multiple fragments at the same time to make up a whole screen. Within an activity, you can also swap out different fragments with each other. (You can also have invisible fragments as well, that do some work related to the activity, but we won’t cover those in this course.)

Fragments were introduced in Android when we started building for larger screen devices like tablets. Let me show you an example.

In the master/detail pattern, a list fragment is on the left-side of the screen, while the detail fragment on the right swaps out depending on the list item selected.

Fragments are used to create a master/detail flow. ([Image Source](<a href="http://developer.android.com/guide/components/fragments.html?utm_source=udacity&utm_medium=course&utm_campaign=android_basics#Design" target="_blank">Source</a>))

Fragments are used to create a master/detail flow. ([Image Source](Source))

In this case, using fragments is convenient when adapting the app to smaller devices like phones. When the user opens the app on a phone, they can see the list fragment. If they tap on an item in the list, they can navigate to the detail fragment.

(<a href="http://developer.android.com/guide/components/fragments.html?utm_source=udacity&utm_medium=course&utm_campaign=android_basics#Design" target="_blank">Source</a>)

(Source)

On the phone, only 1 fragment is shown at a time. On the tablet, 2 fragments are shown beside each other at the same time, to take advantage of the larger screen real estate available.

The number of fragments that appear on a device differ depending on device screen real estate. ([Image Source](<a href="http://developer.android.com/training/basics/fragments/fragment-ui.html?utm_source=udacity&utm_medium=course&utm_campaign=android_basics" target="_blank">Source</a>))

The number of fragments that appear on a device differ depending on device screen real estate. ([Image Source](Source))

This is just one use case for fragments. You can split up the logic of your app into as many fragments as you want. However, splitting it up into too many fragments can cause extra overhead because your Activity will need to manage communication between the fragments.

A Fragment is a Java class. You create your own Fragment, just like you created your own activities. You subclass the Fragment class in the Android framework.

Review Inheritance

Remember earlier we discussed the concept of inheritance in Java in which subclasses are derived from a superclass. In our original code, the Activity class created by the Android framework is the superclass and classes we created such as the MainActivity or NumbersActivity are subclasses that inherit from the Activity class.

Activity is the super class. MainActivity, NumbersActivity, FamilyActivity, etc are sub classes that inherit from the super class.

Activity is the super class. MainActivity, NumbersActivity, FamilyActivity, etc are sub classes that inherit from the super class.

Similarly, the Fragment class is a class provided by the Android framework. The fragments we've create - NumbersFragment, ColorsFragment, etc - are subclasses that inherit from the Fragment super class.

Fragment is the super class. NumberFragament, FamilyFragment, ColorsFragment, etc. are sub classes that inherit from the super class.

Fragment is the super class. NumberFragament, FamilyFragment, ColorsFragment, etc. are sub classes that inherit from the super class.